package Game_Start;

import chart.IO;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import java.io.IOException;

import java.util.Iterator;
import java.util.Set;
import java.awt.*;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;

import Main.Main;

import snake.Snake_body;
  
public class Win extends JFrame{
	private static final long serialVersionUID = 3964892148596844386L;
	private BufferedImage  backgroundImg;//声明对象用于存储图片
	 private static Clip clip;
	 private Process process;
	 public static Win Frame;
	    
	 public  Win() throws IOException {
		 
	        // 读取图片作为背景
		 backgroundImg = ImageIO.read(new File("Sound//测试.jpg"));
	        // 加JPanel组件，放置其他组件和绘制背景图片
	        JPanel contentPane = new JPanel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = -6450222054005758038L;

				@Override
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                // 将背景图绘制到整个panel上，保证填充整个窗口
	                g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
	            }
	        };
	        setContentPane(contentPane);
	    }
	 public void  Win1() throws IOException {
	        // 读取图片作为背景
	        backgroundImg = ImageIO.read(new File("Sound/帮助.png"));
	        // 加JPanel组件，放置其他组件和绘制背景图片
	        JPanel contentPane = new JPanel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = -9222551260935065197L;

				@Override
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                // 将背景图绘制到整个panel上，保证填充整个窗口
	                g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
	            }
	        };
	        setContentPane(contentPane);
	    } 
	 public void Win2() throws IOException {
	        // 读取图片作为背景
	        backgroundImg = ImageIO.read(new File("Sound/退出.jpg"));
	        // 加JPanel组件，放置其他组件和绘制背景图片
	        JPanel contentPane = new JPanel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = -4133621221729480138L;

				@Override
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                // 将背景图绘制到整个panel上，保证填充整个窗口
	                g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
	            }
	        };
	        setContentPane(contentPane);
	    }
	 public void  Win3() throws IOException {
	        // 读取图片作为背景
	        backgroundImg = ImageIO.read(new File("Sound/声音.jpg"));
	        // 加JPanel组件，放置其他组件和绘制背景图片
	        JPanel contentPane = new JPanel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = -9222551260935065197L;

				@Override
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                // 将背景图绘制到整个panel上，保证填充整个窗口
	                g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
	            }
	        };
	        setContentPane(contentPane);
	    } 
	 public void  Win4() throws IOException {
	        // 读取图片作为背景
	        backgroundImg = ImageIO.read(new File("Sound/排行榜.jpg"));
	        // 加JPanel组件，放置其他组件和绘制背景图片
	        JPanel contentPane = new JPanel() {
	            /**
				 * 
				 */
				private static final long serialVersionUID = -9222551260935065197L;

				@Override
	            protected void paintComponent(Graphics g) {
	                super.paintComponent(g);
	                // 将背景图绘制到整个panel上，保证填充整个窗口
	                g.drawImage(backgroundImg, 0, 0, getWidth(), getHeight(), null);
	            }
	        };
	        setContentPane(contentPane);
	    } 
	 public void Sound() {
		    setLocation(600,500);
			setSize(428,290);
	        //setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	        setLocationRelativeTo(null);
	        
	        JButton playButton = new JButton();
			  JButton stopButton = new JButton();//创建播放和停止按钮并创建监听器调用方法
	        playButton.addActionListener(e -> playMusic());
	        stopButton.addActionListener(e  ->stopMusic());
	        //JPanel buttonPanel = new JPanel();
	       // playButton.setBackground(Color.white);
	        playButton.setBorderPainted(false);
	        playButton.setContentAreaFilled(false);//背景不填充颜色
	        playButton.setFont(new  java.awt.Font("华文新魏",  1,  15));
	       // stopButton.setBackground(Color.pink);
	        stopButton.setBorderPainted(false);
	        stopButton.setContentAreaFilled(false);
	        stopButton.setFont(new  java.awt.Font("华文新魏",  1,  15));
			
	        this.add(playButton);
	        this.add(stopButton);
	        //add(buttonPanel);
	        //setVisible(true);
	    }
	    public static void playMusic() {
	        try {
	            // load audio file
	            File audioFile = new File("Sound\\背景音乐.wav");
	            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
	            
	            if(clip==null)//如果音频剪辑为空，则创建新的音频剪辑
	            clip = AudioSystem.getClip();
	            else return ;
	            
	            clip.open(audioStream);// 打开音频剪辑并设置循环播放
	            clip.loop(Clip.LOOP_CONTINUOUSLY);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	    
	    public static void stopMusic() {
	    	
	    	if(clip==null) return ;// 如果音频剪辑为空，则直接返回
	        clip.stop();// 停止音频剪辑播放
	        clip=null;
	        
	    }
	    public static void set_Frame(Win frame)//设置主窗体
	    {
	    	Win.Frame=frame;
	    }
	    public static Win getFrame()//获取主窗体对象
	    {
	    	return Win.Frame;
	    }

	public static void main(String[] args) throws IOException {// TODO 自动生成的方法存根
		Win frame=new Win();        //窗口对象
		Win.set_Frame(frame);     
		
	    Win set=new Win();             //	  设置窗口
		set.setTitle("游戏设置");        
		set.setLocation(600,500);
		set.setSize(320,225);
		JButton h=new JButton("游戏帮助");
		JButton s=new JButton("音效");
		h.setBackground(Color.white);
		h.setBorderPainted(false);                  //去除按钮边框
		h.setFont(new  java.awt.Font("华文新魏",  1,  15));
		s.setBackground(Color.pink);
		s.setBorderPainted(false);                      //去除按钮边框
		s.setFont(new  java.awt.Font("华文新魏",  1,  15));
		set.setLayout(new GridLayout(1,2));          //网格布局
		 set.add(h);
		 set.add(s);
		 
		 Win sound=new Win();                //背景音乐窗口
		 sound.Win3();                          
		 sound.setTitle("游戏音效");
		 sound.setLayout(new  GridLayout(1,2));
		 sound.Sound();                    //调用音乐控制开关窗口
		 sound.playMusic();
		 
		
		JButton y=new JButton("是");          
		y.setBackground(Color.white);
		y.setBorderPainted(false);
		y.setFont(new  java.awt.Font("华文新魏",  1,  15));
		JButton n=new JButton("否");
		n.setBackground(Color.pink);
		n.setFont(new  java.awt.Font("华文新魏",  1,  15));
		n.setBorderPainted(false);
		Win choice=new Win();                //选择退出窗口
		choice.Win2();                   
        choice.setTitle("是否要退出游戏？");           // 设置窗口标题和初始大小
		 choice.setLayout(new GridLayout(5,5));
		 
		 choice.add(y);
		 for(int i=1;i<=4;i++) {
			  JPanel placeholder1 = new JPanel();//添加面板并隐藏
		  placeholder1.setVisible(false);
		   choice.add(placeholder1);
		  }
		choice.add(n);
		for(int i=1;i<=19;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   choice.add(placeholder1);
		  }
		
		choice.setLocation(600,500);
		choice.setSize(320,225);
		//choice.setVisible(true);
		
		Win help=new Win();               //游戏帮助窗口
		help.setTitle("游戏帮助"); 
		help.Win1();
		help.setBounds(30, 60, 80, 40);
		//Box box1=Box.createVerticalBox();        
		//help.add(box1);
		help.setLocation(500, 300);
		help.setSize(500,300);
//创建按钮
		
		JButton b1=new JButton("开始游戏");
		JButton b2=new JButton("继续游戏");
		JButton b3=new JButton("排行榜");
		JButton b4=new JButton("退出游戏");
		JButton b5=new JButton("游戏设置");
		
		//b1.setContentAreaFilled(false);b2.setContentAreaFilled(false);
		//b3.setContentAreaFilled(false);b4.setContentAreaFilled(false);b5.setContentAreaFilled(false);
		b1.setFont(new  java.awt.Font("华文新魏",  1,  15));
		b1.setBackground(Color.white);
		b1.setBorderPainted(false);
		//b1.setContentAreaFilled(false);
		b2.setFont(new  java.awt.Font("华文新魏",  1,  15));
		b2.setBackground(Color.pink);
		b2.setBorderPainted(false);
		//b2.setContentAreaFilled(false);
		b3.setFont(new  java.awt.Font("华文新魏",  1,  15));
		b3.setBackground(Color.red);
		b3.setBorderPainted(false);
		//b3.setContentAreaFilled(false);
		b4.setFont(new  java.awt.Font("华文新魏",  1,  15));
		b4.setBackground(Color.white);
		b4.setBorderPainted(false);
		//b4.setContentAreaFilled(false);
		b5.setFont(new  java.awt.Font("华文新魏",  1,  15));
		b5.setBackground(Color.pink);
		b5.setBorderPainted(false);
		//b5.setContentAreaFilled(false);
		b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				frame.hide();
				new Thread() {
					public void run()
					{
						Main.Game(null);
					}
				}.start();
			}});
		b2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Snake_body snake=new Snake_body();
					FileIO.IO.IN(snake);
					frame.hide();
					new Thread() { 
						public void run()
						{
							Main.Game(snake);
						}
					}.start();
				
			}});
		GridLayout layout=new GridLayout(5,8);       ///网格布局
		 frame.setLayout(layout);
		 for(int i=1;i<=7;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   frame.add(placeholder1);
		  }
		 frame.add(b1);
		 for(int i=1;i<=7;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   frame.add(placeholder1);
		  }
		
		  frame.add(b2);
		  for(int i=1;i<=7;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   frame.add(placeholder1);
		  }
		  frame.add(b3);
		  for(int i=1;i<=7;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   frame.add(placeholder1);
		  }
		  frame.add(b5);
		  for(int i=1;i<=7;i++) {
			  JPanel placeholder1 = new JPanel();
		  placeholder1.setVisible(false);
		   frame.add(placeholder1);
		  }
		  frame.add(b4);
		  Game_Start.Win frameo=new Win();          //排行榜窗口
			frameo.Win4();
			frameo.setLayout(new GridLayout(11,1));// 网格布局
			JLabel[] labels = new JLabel[11];
			labels[0]=new JLabel();
			labels[0].setText("               排行榜"+"                    "+"             分数");
			frameo.add(labels[0]);
			//IO other  =new IO();
			
				
			frameo.setLocation(550,100);
	        frameo.setTitle("排行榜");// 设置窗口标题和初始大小
			frameo.setSize(300,600);
			frameo.setVisible(false);
			Set<Double> dataset = IO.out();       //引用IO类里的集合
			
			for(int x=1;x<11;x++)
			{
				labels[x]=new JLabel();          //一次性创建完所有标签对象
				frameo.add(labels[x]);
			}
			//frameo.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		  b3.addActionListener(//添加点击后的行为
				  new ActionListener(){
				  		@Override
				  		public void actionPerformed(ActionEvent e){
				  			
							Iterator<Double> it = dataset.iterator();//令treeset使用迭代器降序排列
							int n1=1;
							int j=1;
						if(!(it.hasNext())) {
								for(int i=1;i<10;i++) {
									
									labels[i].setText("                  "+
									i
									+"                                        0.0");
									
								}
								labels[10]=new JLabel();
								labels[10].setText("                  "+
									"10"
									+"                                       0.0");
								
							}
								System.out.println(dataset);
								
								while((it.hasNext())&&(j<11)) {
								    double ele = it.next();
								    
								    labels[j].setText("                     "+j+"                                     "
											+ele);
								    
								    
								    
								    if(j==10) {
								    	labels[j].setText("                    "+j+
								    			"                                    "
								    			+ele);
								    	
								    }
								    
							    j++;
							    }
				  			frameo.setVisible(true);
				  			}
				  		});
		b4.addActionListener(//添加点击后的行为
new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e){
			 choice.setVisible(true);
			}
		});
		b5.addActionListener(//添加点击后的行为
new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e){
			 set.setVisible(true);
			}
		});
		y.addActionListener(//添加点击后的行为
				new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e){
							
							 choice.dispose();
							 frame.dispose();
							 set.dispose();
							 help.dispose();
							 sound.dispose();
							 System.exit(0);
							}
						});
		n.addActionListener(//添加点击后的行为
				new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e){
							 choice.dispose();
							}
						});
		h.addActionListener(//添加点击后的行为
				new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e){
							 help.setVisible(true);
							 set.dispose();
							}
						});
		s.addActionListener(//添加点击后的行为
				new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e){
							 sound.setVisible(true);
							 set.dispose();
	
							}
						});
		frame.setLocation(300,200);
        frame.setTitle("贪吃蛇小游戏");// 设置窗口标题和初始大小
		frame.setSize(1000,600);
		frame.setIconImage(new ImageIcon("img\\1111.jpg").getImage());//设置窗口小图标
		frame.setVisible(true);
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		
	}
	
//	public static void Add_Component(JFrame jfr,GridBagLayout gbl,Component comp,GridBagConstraints gbc,int gridx,int gridy,int gridheight,int gridwidth,int weight_x,int weight_y)
//	{
//		gbc.weightx=weight_x;
//		gbc.weighty=weight_y;
//		gbc.gridheight=gridheight;
//		gbc.gridwidth=gridwidth;
//		gbc.gridx=gridx;
//		gbc.gridy=gridy;
//		gbl.setConstraints(comp, gbc);
//		jfr.add(comp);
//	}
}
